package com.sskj.lib.util;

import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.text.Html;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.afollestad.materialdialogs.MaterialDialog;
import com.sskj.common.base.App;
import com.sskj.common.util.ClickUtil;
import com.sskj.common.util.ToastUtil;
import com.sskj.lib.R;
import com.sskj.lib.widget.RudessMaterialDialog;

public class TipUtil {


    /**
     * 公告弹窗
     *
     * @param context
     * @param content
     */
    public static void showGonggao(Context context, String content) {
        MaterialDialog dialog = new RudessMaterialDialog.Builder(context)
                .customView(R.layout.lib_dialog_gonggao, false)
                .autoDismiss(false)
                .cancelable(false)
                .show();
        View customeView = dialog.getCustomView();

        ((TextView) customeView.findViewById(R.id.tvContent)).setText(content);
        ClickUtil.click(customeView.findViewById(R.id.btSure), () -> {
            dialog.dismiss();
        });
    }

    public static MaterialDialog getSureTip(Activity activity, String title, String content, String sure, OnTipSure onTipSure) {
        MaterialDialog dialog = new RudessMaterialDialog.Builder(activity)
                .customView(R.layout.lib_dialog_sure, false)
                .autoDismiss(true)
                .cancelable(true)
                .show();
        View customView = dialog.getCustomView();
        ((ViewGroup) (customView.getParent())).setBackgroundColor(Color.TRANSPARENT);
        ((ViewGroup) (customView.getParent().getParent())).setBackgroundColor(Color.TRANSPARENT);
        ((ViewGroup) (customView.getParent().getParent().getParent())).setBackgroundColor(Color.TRANSPARENT);
        ((ViewGroup) (customView.getParent().getParent().getParent().getParent())).setBackgroundColor(Color.TRANSPARENT);
        ((ViewGroup) (customView.getParent().getParent().getParent().getParent().getParent())).setBackgroundColor(Color.TRANSPARENT);
        TextView tvContent = customView.findViewById(R.id.tvContent);
        TextView tvTitle = customView.findViewById(R.id.tvTitle);
        Button btSure = customView.findViewById(R.id.btSure);
        Button ivClose = customView.findViewById(R.id.ivClose);
        ClickUtil.click(ivClose, () -> {
            dialog.dismiss();
        });
        tvContent.setText(Html.fromHtml(content));
        if (TextUtils.isEmpty(title)) {
            tvTitle.setVisibility(View.GONE);
        } else {

        }
        tvTitle.setText(title);
        btSure.setText(sure);

        ClickUtil.click(btSure, () -> {
            dialog.dismiss();
            onTipSure.onSure();
        });
        return dialog;
    }

    public static MaterialDialog getExitTip(Activity activity, String content, OnTipSure onTipSure) {
        MaterialDialog dialog = new RudessMaterialDialog.Builder(activity)
                .customView(R.layout.lib_dialog_exit, false)
                .autoDismiss(false)
                .cancelable(false)
                .show();
        View customView = dialog.getCustomView();
        ((ViewGroup) (customView.getParent())).setBackgroundColor(Color.TRANSPARENT);
        ((ViewGroup) (customView.getParent().getParent())).setBackgroundColor(Color.TRANSPARENT);
        ((ViewGroup) (customView.getParent().getParent().getParent())).setBackgroundColor(Color.TRANSPARENT);
        ((ViewGroup) (customView.getParent().getParent().getParent().getParent())).setBackgroundColor(Color.TRANSPARENT);
        ((ViewGroup) (customView.getParent().getParent().getParent().getParent().getParent())).setBackgroundColor(Color.TRANSPARENT);
        TextView tvContent = customView.findViewById(R.id.tvContent);
        TextView tvTitle = customView.findViewById(R.id.tvTitle);
        TextView btSure = customView.findViewById(R.id.btSure);
        ImageView ivClose = customView.findViewById(R.id.ivClose);
        ivClose.setVisibility(View.GONE);
        ClickUtil.click(ivClose, () -> {
            dialog.dismiss();
        });
        tvContent.setText(content);
        tvTitle.setText(App.INSTANCE.getString(R.string.liblogoutProviderImpl1));
        btSure.setText(App.INSTANCE.getString(R.string.libtipUtil2));
        ClickUtil.click(btSure, () -> {
            dialog.dismiss();
            onTipSure.onSure();
        });
        return dialog;
    }

    public static MaterialDialog getSureTip(Activity activity, String title, String content, OnTipSure onTipSure) {
        return getSureTip(activity, title, content, App.INSTANCE.getString(R.string.libtipUtil2), onTipSure);
    }

    public static MaterialDialog getSureTip(Activity activity, String content, OnTipSure onTipSure) {
        return getSureTip(activity, App.INSTANCE.getString(R.string.liblogoutProviderImpl1), content, App.INSTANCE.getString(R.string.libtipUtil2), onTipSure);
    }

    public static MaterialDialog getSureCancelTip(Activity activity, String content, OnTipSure onTipSure) {
        return getSureCancelTip(activity, App.INSTANCE.getString(R.string.liblogoutProviderImpl1), content, onTipSure);
    }

    public static MaterialDialog getSureCancelTip(Activity activity, String title, String content, OnTipSure onTipSure) {
        return getSureCancelTip(activity, title, content, App.INSTANCE.getString(R.string.libtipUtil3), onTipSure);
    }

    public static MaterialDialog getSureCancelTip(Activity activity, String title, String content, String sure, OnTipSure onTipSure) {
        MaterialDialog dialog = new RudessMaterialDialog.Builder(activity)
                .customView(R.layout.lib_dialog_sure_cancel, false)
                .autoDismiss(true)
                .cancelable(true)
                .show();
        View customView = dialog.getCustomView();
        ((ViewGroup) customView.getParent()).setBackgroundColor(Color.TRANSPARENT);
        ((ViewGroup) customView.getParent().getParent()).setBackgroundColor(Color.TRANSPARENT);
        ((ViewGroup) customView.getParent().getParent().getParent()).setBackgroundColor(Color.TRANSPARENT);
        ((ViewGroup) customView.getParent().getParent().getParent().getParent()).setBackgroundColor(Color.TRANSPARENT);
        ((ViewGroup) customView.getParent().getParent().getParent().getParent().getParent()).setBackgroundColor(Color.TRANSPARENT);
        TextView tvContent = customView.findViewById(R.id.tvContent);
        TextView tvTitle = customView.findViewById(R.id.tvTitle);
        TextView tvSure = customView.findViewById(R.id.tvSure);
        TextView btSure = customView.findViewById(R.id.btSure);
        ImageView ivClose = customView.findViewById(R.id.ivClose);
        tvSure.setText(sure);
        btSure.setText(sure);
        if (!TextUtils.isEmpty(title)) {
            tvTitle.setVisibility(View.VISIBLE);
            tvTitle.setText(title);
        }
        tvContent.setText(content);
        ClickUtil.click(dialog.getCustomView().findViewById(R.id.tvSure), () -> {
            dialog.dismiss();
            onTipSure.onSure();
        });
        ClickUtil.click(dialog.getCustomView().findViewById(R.id.btSure), () -> {
            dialog.dismiss();
            onTipSure.onSure();
        });
        ClickUtil.click(dialog.getCustomView().findViewById(R.id.tvCancel), () -> {
            dialog.dismiss();
        });
        ClickUtil.click(dialog.getCustomView().findViewById(R.id.ivClose), () -> {
            dialog.dismiss();
        });
        return dialog;
    }

    public static MaterialDialog tipsDialog(Activity activity, OnTipSure2 tipSure2) {
        MaterialDialog dialog = new RudessMaterialDialog.Builder(activity)
                .customView(R.layout.lib_dialog2_sure_cancel, false)
                .autoDismiss(true)
                .cancelable(true)
                .show();
        View customView = dialog.getCustomView();
        ((ViewGroup) customView.getParent()).setBackgroundColor(Color.TRANSPARENT);
        ((ViewGroup) customView.getParent().getParent()).setBackgroundColor(Color.TRANSPARENT);
        ((ViewGroup) customView.getParent().getParent().getParent()).setBackgroundColor(Color.TRANSPARENT);
        ((ViewGroup) customView.getParent().getParent().getParent().getParent()).setBackgroundColor(Color.TRANSPARENT);
        ((ViewGroup) customView.getParent().getParent().getParent().getParent().getParent()).setBackgroundColor(Color.TRANSPARENT);
        TextView tvContent = customView.findViewById(R.id.content);
        TextView tvSure = customView.findViewById(R.id.cancel);
        TextView tvCancel = customView.findViewById(R.id.sure);

        ClickUtil.click(tvSure, () -> {
            tipSure2.onSure();
            dialog.dismiss();
        });
        ClickUtil.click(tvCancel, tipSure2::onCancel);
        return dialog;
    }


    public static MaterialDialog tipsDialogChargeCoin(Activity activity, OnTipSure3 tipSure3, String fee) {
        MaterialDialog dialog = new RudessMaterialDialog.Builder(activity)
                .customView(R.layout.lib_dialog_sure_cancel_charge_coin, false)
                .autoDismiss(true)
                .cancelable(true)
                .show();
        View customView = dialog.getCustomView();
        ((ViewGroup) customView.getParent()).setBackgroundColor(Color.TRANSPARENT);
        ((ViewGroup) customView.getParent().getParent()).setBackgroundColor(Color.TRANSPARENT);
        ((ViewGroup) customView.getParent().getParent().getParent()).setBackgroundColor(Color.TRANSPARENT);
        ((ViewGroup) customView.getParent().getParent().getParent().getParent()).setBackgroundColor(Color.TRANSPARENT);
        ((ViewGroup) customView.getParent().getParent().getParent().getParent().getParent()).setBackgroundColor(Color.TRANSPARENT);
        TextView tvContent = customView.findViewById(R.id.content);
        TextView tvSure = customView.findViewById(R.id.cancel);
        TextView tvCancel = customView.findViewById(R.id.sure);
        TextView tvDefault = customView.findViewById(R.id.tvAccountDefault);
        EditText etInput = customView.findViewById(R.id.et_account_input);
        EditText etPaswd = customView.findViewById(R.id.et_paswd_input);
        tvContent.setText(String.format(App.INSTANCE.getString(R.string.libtipUtil4), fee));
        ClickUtil.click(tvSure, () -> {

            if (TextUtils.isEmpty(etInput.getText())) {
                ToastUtil.showShort(tvDefault.getText().toString() + etInput.getHint());
                return;
            }

            if (TextUtils.isEmpty(etPaswd.getText())) {
                ToastUtil.showShort(etPaswd.getHint());
                return;
            }

            if (!PatternUtils.isPayPs(etPaswd.getText().toString())) {
                ToastUtil.showShort(App.INSTANCE.getString(R.string.libtipUtil5));
                return;
            }

            tipSure3.onSure(etInput.getText().toString(), etPaswd.getText().toString());
            dialog.dismiss();


        });
        ClickUtil.click(tvCancel, () -> dialog.dismiss());
        return dialog;
    }


    public interface OnTipSure {
        void onSure();

    }

    public interface OnTipSure2 {
        void onSure();

        void onCancel();
    }


    public interface OnTipSure3 {
        void onSure(String account, String paswd);

        void onCancel();
    }
}
